package entity;

import java.util.*;
import entity.Database.DataType;

public class Table 
{
	private String Name;
	private List<Column> columns;
	private Integer primaryKeyIndex;
	private int rowCount;
	
	public Table()
	{
		columns= new ArrayList<Column>();
		primaryKeyIndex= -1;
		rowCount = 0;
	}
	
	public Table(String Name)
	{
		columns= new ArrayList<Column>();
		primaryKeyIndex = -1;
		this.Name = Name;
		rowCount = 0;
	}
	
	public String getName()
	{
		return this.Name;
	}
	
	public void setName(String name)
	{
		this.Name = name;
	}
	
	public boolean addColumn(Column newColumn)
	{
		if (getColumnByName(newColumn.getName())==null) 
		{
			this.columns.add(newColumn);
			return true;
		}
		else return false;
	}
	
	public Column getColumnAt(Integer index)
	{
		Column result=null;
		try 
		{
			result = this.columns.get(index);
			return result;
		}
		catch ( IndexOutOfBoundsException IOOBE)
		{
			return result;
		}
	}
	public Column getColumnByName(String name)
	{
		for (int i=0;i<columns.size();i++) if (columns.get(i).getName().compareTo(name)==0) return columns.get(i);
		return null;
	}
	
	public int getColumnIndexByName(String name)
	{
		for (int i=0;i<columns.size();i++) if (columns.get(i).getName().compareTo(name)==0) return i;
		return -1;
	}
	
	public boolean editColumn(String oldName , String newName, DataType newType)
	{
		if (getColumnByName(newName)!=null) return true;
		Column column = getColumnByName(oldName);
		column.setName(newName);
		column.setType(newType);
		return true;
	}
	public boolean removeColumnAt(Integer index)
	{
		try 
		{
			this.columns.remove(index);
			return true;
		}
		catch ( IndexOutOfBoundsException IOOBE)
		{
			return false;
		}
	}
	
	public boolean removeColumnByName(String name)
	{
		Column column = getColumnByName(name);
		if (column==null) return false;
		columns.remove(column);
		return true;
	}
	public Integer getColumnsCount()
	{
		return this.columns.size();
	}
	
	public void clearColumns()
	{
		for (int i=0;i<columns.size();i++) columns.get(i).clearData();
		rowCount=0;
	}
	
	public boolean clearDataAt(int index)
	{
		if (index<0||index>rowCount) return false;
		for (int i=0;i<columns.size();i++) columns.get(i).removeDataAt(index);
		rowCount--;
		return true;
	}
	public int getPrimaryKey()
	{
		return primaryKeyIndex;
	}
	
	public boolean setPrimaryKey(int index)
	{
		if (index>=0 && index<columns.size()) 
		{
			primaryKeyIndex = index;
			return true;
		}
		else return false;
	}
	public boolean addRow(ArrayList<String> values)
	{
		if (values.size()!=this.getColumnsCount()) return false;
		for (int i=0;i<this.getColumnsCount();i++) if (!this.getColumnAt(i).addData(values.get(i))) return false;
		this.rowCount++;
		return true;
	}
	
	public int getRowCount()
	{
		return this.rowCount;
	}
	
	public void setRowCount(int newRowCount)
	{
		rowCount = newRowCount;
	}
}
